﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Text.RegularExpressions;

public partial class Business_numbercontrol : System.Web.UI.Page
{
    PM.PMBusiness.projectschedual a = new PM.PMBusiness.projectschedual();//实例化
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            gvDataBindNew();//绑定gridview
            if (Session["plancode"] != null)//即为刚跳转过来的新计划第二步
            {
                //获取数据
                this.undoneJob.InnerHtml = a.GetData(this.ddlPlanCodeN, this.ddlPlanCode, true);
                this.btnSubmit.Visible = true;
                this.ddlPlanCodeN.Enabled = false;
                this.ddlPlanCode.Enabled = false;
            }
            else
            {
                //获取数据
                this.undoneJob.InnerHtml = a.GetData(this.ddlPlanCodeN, this.ddlPlanCode, false);
                this.ddlPlanCodeN.Enabled = true;
                this.ddlPlanCode.Enabled = true;
            }
            //ddlDataBind(true);
            //ddlDataBind(false);
            this.ddlPlanCode.Items.Insert(0, "项目编号");
            this.ddlPlanCodeN.Items.Insert(0, "项目编号");
            this.ddlPlanCodeN.Attributes.Add("onchange", "ddlnchange()");
            this.ddlPlanCode.Attributes.Add("onchange", "ddlchange()");
        }
    }

    protected void gvDataBindNew()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("select collegeid,collegename,0 as number from Dict_College order by collegeid");
        DataTable dt = PM.Code.DAL.DbHelper.ExecuteDataTableBySql(sb.ToString());
        gvConfigNumber.DataSource = dt;
        gvConfigNumber.DataBind();
    }

    protected void gvDataBind()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("select id,collegeid,collegename,number,builddate from View_NumberControl ");
        sb.AppendFormat("where plancode='{0}' order by id",ddlPlanCode.SelectedValue.Trim()!=string.Empty?ddlPlanCode.SelectedValue.Trim():"0");
        DataTable dt = PM.Code.DAL.DbHelper.ExecuteDataTableBySql(sb.ToString());
        gvConfigNumber.DataSource = dt;
        gvConfigNumber.DataBind();
        gvConfigNumber.DataKeyNames = new String[] { "id" };
    }

    //protected void ddlDataBind(bool status)
    //{
    //    StringBuilder sqlstr = new StringBuilder();
    //    sqlstr.Append("select plancode from Bus_ProjectPlan ");
    //    if (status)
    //    {
    //        sqlstr.AppendFormat("where opendate>'{0}' and pstatus='已完成'", DateTime.Now.ToString());
    //        PM.Code.Web.controlbind.BindDropDownList(this.ddlPlanCode, sqlstr.ToString(), "plancode", "plancode");
    //    }
    //    else
    //    {
    //        sqlstr.AppendFormat("where opendate>'{0}' and pstatus='未完成'", DateTime.Now.ToString());
    //        PM.Code.Web.controlbind.BindDropDownList(this.ddlPlanCodeN, sqlstr.ToString(), "plancode", "plancode");
    //    }
    //}
    protected void ddlPlanCode_SelectedIndexChanged(object sender, EventArgs e)
    {
            gvDataBind();
    }
    //编辑行
    protected void gvConfigNumber_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
    {
        this.gvConfigNumber.EditIndex = e.NewEditIndex;
        gvDataBind();
    }
    //取消行编辑
    protected void gvConfigNumber_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.gvConfigNumber.EditIndex = -1;
        gvDataBind();
    }
    //更新行
    protected void gvConfigNumber_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //获取行主键值
        int id = Convert.ToInt16(gvConfigNumber.DataKeys[e.RowIndex].Values["id"].ToString().Trim());
        //获取修改值
        string number = ((TextBox)gvConfigNumber.Rows[e.RowIndex].FindControl("TextBox1")).Text.Trim();
        if (number == string.Empty)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "数据不能为空");
            return;
        }
        //正则表达式进行数字验证
        if (!Regex.IsMatch(number, "^+?[1-9][0-9]*$"))
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "只能输入整数！");
            return;
        }

        //进行更新
        if (a.Update_Number(id, Int32.Parse(number)) == 1)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "更新成功！");
            this.gvConfigNumber.EditIndex = -1;
            gvDataBind();
        }
    }
    //提交
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //清除session["plancode"]并刷新本页
        string plancode = "";
        if (Session["plancode"] != null)
        {
            plancode = Session["plancode"].ToString();
            Session.Remove("plancode");//删除会话
        }
        else if (this.ddlPlanCodeN.SelectedIndex != 0)
            plancode = this.ddlPlanCodeN.SelectedValue.Trim();
        else
        {
            //返回错误提示
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "对不起，您未选定任何有效计划编号！");
            return;
        }
        a.AddNumber(this.gvConfigNumber, plancode);//插入所有并修改已完成字段
        ddlPlanCode.Items.Add(new ListItem(plancode,plancode));
        ddlPlanCode.SelectedIndex = ddlPlanCode.Items.Count - 1;
        ddlPlanCodeN.Items.Remove(ddlPlanCode.SelectedValue.Trim());
        gvDataBind();
        ddlPlanCodeN.Enabled = true;
        ddlPlanCode.Enabled = true;
    }
}
